'use strict';

var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');
var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard');
Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require('@babel/runtime/helpers/objectSpread2'));
var _toConsumableArray2 = _interopRequireDefault(
  require('@babel/runtime/helpers/toConsumableArray'),
);
var _objectWithoutProperties2 = _interopRequireDefault(
  require('@babel/runtime/helpers/objectWithoutProperties'),
);
var _react = _interopRequireWildcard(require('react'));
var _antd = require('antd');
var _createField = _interopRequireDefault(require('../../../createField'));
var _FieldContext = require('../../../contexts/FieldContext');
var _excluded = ['options', 'fieldNames', 'authedBy', 'value', 'onChange'];
var AuthedCascader = function AuthedCascader(_ref) {
  var options = _ref.options,
    fieldNames = _ref.fieldNames,
    authedBy = _ref.authedBy,
    value = _ref.value,
    onChange = _ref.onChange,
    props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var _useFieldContext = (0, _FieldContext.useFieldContext)(),
    label = _useFieldContext.label;
  var _ref2 = fieldNames || {},
    _ref2$value = _ref2.value,
    valueKey = _ref2$value === void 0 ? 'value' : _ref2$value,
    _ref2$label = _ref2.label,
    labelKey = _ref2$label === void 0 ? 'label' : _ref2$label,
    _ref2$children = _ref2.children,
    childrenKey = _ref2$children === void 0 ? 'children' : _ref2$children;
  var innerOptions = (0, _react.useMemo)(
    function () {
      if (authedBy === true)
        return {
          readonlyOptions: [],
          configOptions: options,
        };
      var readonlyOptions = [];
      var configOptions = [];
      var travser = function travser(opts) {
        var level = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
        var matched = opts.find(function (opt) {
          return authedBy && opt[valueKey] === authedBy[level];
        });
        if (!matched) return;
        if (level + 1 === (authedBy === null || authedBy === void 0 ? void 0 : authedBy.length)) {
          configOptions = matched[childrenKey];
        }
        if (level < authedBy.length) {
          readonlyOptions.push(matched);
          if (matched.children) travser(matched.children, level + 1);
        }
      };
      // @ts-ignore
      travser(options);
      return {
        readonlyOptions: readonlyOptions,
        configOptions: configOptions,
      };
    },
    [options, authedBy],
  );
  var readonlyValues = (0, _react.useMemo)(
    function () {
      var readonlyOptions = innerOptions.readonlyOptions;
      return readonlyOptions.map(function (item) {
        return item[valueKey];
      });
    },
    [innerOptions],
  );
  var readonlyLabels = (0, _react.useMemo)(
    function () {
      var readonlyOptions = innerOptions.readonlyOptions;
      return readonlyOptions.map(function (item) {
        return item[labelKey];
      });
    },
    [innerOptions],
  );
  var configOptions = (0, _react.useMemo)(
    function () {
      return innerOptions.configOptions;
    },
    [innerOptions],
  );
  var handleChange = (0, _react.useCallback)(
    function (value) {
      // @ts-ignore
      if (onChange)
        onChange(
          !readonlyValues.length
            ? value
            : [].concat(
                (0, _toConsumableArray2.default)(readonlyValues),
                (0, _toConsumableArray2.default)(value),
              ),
        );
    },
    [readonlyValues, onChange],
  );
  return /*#__PURE__*/ _react.default.createElement(
    _react.Fragment,
    null,
    readonlyLabels.length
      ? /*#__PURE__*/ _react.default.createElement('span', null, readonlyLabels.join(''))
      : '',
    configOptions.length
      ? /*#__PURE__*/ _react.default.createElement(
          _antd.Cascader,
          (0, _objectSpread2.default)(
            (0, _objectSpread2.default)(
              {
                options: configOptions,
                placeholder: '\u8BF7\u9009\u62E9'.concat(label || ''),
                style: !readonlyLabels.length
                  ? undefined
                  : {
                      width: '72%',
                      marginLeft: 5,
                    },
              },
              props,
            ),
            {},
            {
              value:
                value !== undefined
                  ? value.slice(!readonlyValues.length ? 0 : readonlyValues.length)
                  : undefined,
              onChange: handleChange,
            },
          ),
        )
      : null,
  );
};
var _default = (0, _createField.default)(AuthedCascader);
exports.default = _default;
